package com.example.controller;

import com.example.common.Result;
import com.example.common.ResultUtils;
import com.example.dto.ShardUploadCompleteRequest;
import com.example.dto.ShardUploadDetailResponse;
import com.example.dto.ShardUploadInitRequest;
import com.example.dto.ShardUploadPartRequest;
import com.example.service.ShardUploadService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.io.IOException;

/*
@description:分片上传
@ClassName ShardUploadController
@author chen
@create 2025-05-13 22:48
@Version 1.0
*/
@RestController
@RequestMapping("/shardUpload")
public class ShardUploadController
{
    @Resource
    private ShardUploadService shardUploadService;

    /*
     * @param request
     * @==============History===============<br/>
     * @Description //   创建分片上传任务
     * @Date 2025-5-13 22:54
     * @Author < a href="15991488967@163.com">chen</ a>
     * @return com.practical.dto.Result<java.lang.String>
     */
    @PostMapping("/init")
    public Result<String> init(@RequestBody ShardUploadInitRequest request)
    {
        String shardUploadId = this.shardUploadService.init(request);
        return ResultUtils.ok(shardUploadId);
    }



    /*
     * @param request
     * @==============History===============<br/>
     * @Description //   上传分片(客户端需遍历上传分片文件)
     * @Date 2025-5-13 23:06
     * @Author < a href="15991488967@163.com">chen</ a>
     * @return com.practical.dto.Result<java.lang.Boolean>
     */
    @PostMapping("/uploadPart")
    public Result<Boolean> uploadPart(ShardUploadPartRequest request) throws IOException
    {
        this.shardUploadService.uploadPart(request);
        return ResultUtils.ok(true);
    }


    /*
     * @==============History===============<br/>
     * @Description // 合并分片,上传
     * @Date 2025-6-5 23:26
     * @Author < a href="15991488967@163.com">chen</ a>
     * @return
     */
    @PostMapping("/complete")
    public Result<Boolean> complete(@RequestBody ShardUploadCompleteRequest request) throws IOException
    {
        this.shardUploadService.complete(request);
        return ResultUtils.ok(true);
    }




    /*
     * @==============History===============<br/>
     * @Description //   获取分片任务的详细信息(分片是否上传完成、哪些分片文件是否已上传)
     * @Date 2025-6-5 23:44
     * @Author < a href="15991488967@163.com">chen</ a>
     * @return com.practical.dto.Result<com.practical.dto.ShardUploadDetailResponse>
     */
    @GetMapping("/detail")
    public Result<ShardUploadDetailResponse> detail(@RequestParam("shardUploadId") String shardUploadId)
    {
        return ResultUtils.ok(this.shardUploadService.detail(shardUploadId));
    }



}
